/**
 * Created by qunxing.du on 2016/7/28.
 * Copyright 2016 汉威.智慧环保事业部 Limited.
 * All rights reserved.
 */
package com.hw.one.collect.zzcity.handler;

import com.hw.one.collect.data.DataService;
import com.mongodb.BasicDBList;
import org.apache.log4j.Logger;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 *  原始数据处理器
 */
@Component
public class RawDataHandler extends DataService implements IHandler {
    private static Logger log = Logger.getLogger(RawDataHandler.class);
    @Resource
    protected MongoTemplate mongoTemplate;
    @Resource
    private MongoTemplate mongoTemplateDev;
    /**
     * 处理方法
     *
     * @param o
     * @param handlerChain
     * @return
     * @throws Exception
     */
    @Override
    public synchronized Object handle(Object o,String name,  HandlerChain handlerChain) throws Exception {
        BasicDBList list = getBasicDBList(o);
//        log.info("待保存原始数据:"+ name+"," + list.size());
        //插入正式库
//        insertProductData(list,name);
        //插入开发库备份
//        insertDevData(list, name);

        return handlerChain.handle(list.copy(),name,handlerChain);
    }

    /**
     * 插入生产数据
     * @param basicDBList
     * @param name
     */
    private void insertProductData(BasicDBList basicDBList,String name){
        log.info("插入原始数据到生产库");
        mongoTemplate.insert(basicDBList,name);
    }
    /**
     * 插入数据到开发库
     * @param basicDBList
     * @param name
     */
    private void insertDevData(final BasicDBList basicDBList, final String name) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                log.info("插入原始数据到开发");
                mongoTemplateDev.insert(basicDBList, name);
            }
        }).start();
    }
}
